//
//  DataViewController.m
//  PlayerFix
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 DAT. All rights reserved.
//

#import "DataViewController.h"
#import "DataModel.h"
#import "DataMinModel.h"
#import "TeleplayTableViewCell.h"
#import "MovieTableViewCell.h"

@interface DataViewController ()<UITableViewDataSource,UITableViewDelegate,Teleplay,movieTableView>

@property (nonatomic, strong) DataModel *dataModel;

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, assign) CGFloat gao;

@end

@implementation DataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
   
    // 左侧返回按钮
    UIImage *leftImage = [UIImage imageNamed:@"gobackBtn.png"];
    // 取消渲染
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
       
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64)style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView]; 
    self.myTableView.separatorStyle = 0;
    
    
//    [self addHeaderRefresh];
    [self getAfnModel];
}

- (void)leftButtonAction:(UIBarButtonItem *)barButton{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)topView{
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 355, 280)];
    bgImageView.userInteractionEnabled = YES;
    bgImageView.image = [UIImage imageNamed:@"bg_share_large.png"];
    self.myTableView.tableHeaderView = bgImageView;
    
    // 图片
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 140, 200)];
    [myImageView sd_setImageWithURL:[NSURL URLWithString:self.dataModel.poster] placeholderImage:nil];
    [bgImageView addSubview:myImageView];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 15, 255, 15)];
    titleLabel.text = self.dataModel.title;
    [bgImageView addSubview:titleLabel];
//    NSLog(@"========%@",self.dataModel.title);
    
    // 星星
    /**********评分星星***********/
    UIImageView *imageView1 = [[UIImageView alloc]init];
    imageView1.frame = CGRectMake(160, 40, 75, 15);
    imageView1.image = [UIImage imageNamed:@"rating_gray"];
    [bgImageView addSubview:imageView1];
    CGFloat ratingFloat = [self.dataModel.rating floatValue];
    float bi = ratingFloat / 10.0;
    
    UIImageView *imageTemp2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"rating"]];
    UIImage  *image1  = [UIImage imageNamed:@"rating"];
    
    CGImageRef imageRef  = image1.CGImage;
    
    CGImageRef image2 = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, imageTemp2.frame.size.width*bi*2, imageTemp2.frame.size.height*2));
    
    UIImageView *imageView2 = [[UIImageView alloc]init];
    imageView2.frame = CGRectMake(0, 0, imageView1.frame.size.width * bi, imageView1.frame.size.height);
    imageView2.image = [UIImage imageWithCGImage:image2];
    
    [imageView1 addSubview:imageView2];
    /**********评分星星***********/
    
    NSString *rating = [NSString stringWithFormat:@"(%@) %@ 人评",self.dataModel.rating,self.dataModel.rating_count];
    UILabel *ratingLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 40, 120, 15)];
    ratingLabel.text = rating;
    ratingLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:ratingLabel];
    
    NSString *year = [NSString stringWithFormat:@"年份:%@  地区:%@",self.dataModel.year,self.dataModel.area];
    UILabel *yearLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 65, 200, 10)];
    yearLabel.text = year;
    yearLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:yearLabel];
    
    NSString *director = [NSString stringWithFormat:@"导演:%@",self.dataModel.director];
    UILabel *directorLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 85, 200, 10)];
    directorLabel.text = director;
    directorLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:directorLabel];
    
    NSString *actor = [NSString stringWithFormat:@"主演:%@",self.dataModel.actor];
    UILabel *actorLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 105, 200, 10)];
    actorLabel.text = actor;
    actorLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:actorLabel];
    
    NSString *genres = [NSString stringWithFormat:@"类型:%@",self.dataModel.genres];
    UILabel *genresLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 125, 200, 10)];
    genresLabel.text = genres;
    genresLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:genresLabel];
    
    NSString *release = [NSString stringWithFormat:@"上映:%@  %@",self.dataModel.release_date,self.dataModel.release_area];
    UILabel *releaseLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 145, 200, 10)];
    releaseLabel.text = release;
    releaseLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:releaseLabel];

    NSString *alias_title = [NSString stringWithFormat:@"又名:%@",self.dataModel.alias_title];
    UILabel *alias_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 165, 200, 10)];
    alias_titleLabel.text = alias_title;
    alias_titleLabel.font = [UIFont systemFontOfSize:13];
    [bgImageView addSubview:alias_titleLabel];
    
    UILabel *introLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 , 205, 335, 80)];
    introLabel.text = self.dataModel.intro;
    introLabel.font = [UIFont systemFontOfSize:15];
    introLabel.numberOfLines = 2;
    [bgImageView addSubview:introLabel];
    
}

//- (void)addHeaderRefresh
//{
//    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        [self getAfnModel];
//    }];
//    
//    [self.myTableView.header beginRefreshing];
//}

- (void)getAfnModel{

    NSString *url = [NSString stringWithFormat:@"http://api.gougouvideo.com:8888/detail.php?id=%@&v=1.7.0",_ID];
    NSLog(@"url ==== %@",url);
    [DATAsyncGetAFNBlock GetAFNURLWithString:url andBody:nil andCookie:nil resultBlock:^(id result) {
        
        NSDictionary *bigDic = result;
        self.dataModel = [[DataModel alloc] init];
        // 这个是数组存的是key值  也就是各个播放的优酷之类的key值  youku.com 等
//        self.dataModel.sites = [bigDic objectForKey:@"sites"];
        [self.dataModel setValuesForKeysWithDictionary:bigDic];
        
        NSDictionary *subMoviesDic = [bigDic objectForKey:@"submovies"];
        self.dataModel.submovies = [NSMutableDictionary dictionary];
        for (NSString *key  in subMoviesDic) {
            NSArray *array = [subMoviesDic objectForKey:key];
            NSMutableArray *valueArray = [NSMutableArray array];
            for (NSDictionary *dic in array) {
                DataMinModel *dataMinModel = [[DataMinModel alloc] init];
                [dataMinModel setValuesForKeysWithDictionary:dic];
                [valueArray addObject:dataMinModel];
            }
            [self.dataModel.submovies setObject:valueArray forKey:key];
        }
      
        [self topView];
//        [self.myTableView.header endRefreshing];
        [self.myTableView reloadData];
        
    } errorBlock:^(NSError *error) {
//        [self.myTableView.header endRefreshing];
        NSLog(@"dataError = %@",error);
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataModel.sites.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *key = [_dataModel.sites objectAtIndex:section];
    NSArray *array = [_dataModel.submovies objectForKey:key];
    self.gao = array.count / 4.0 * 35.0 + 80;
    if (array.count == 0) {
        return 0;
    }
    
    return 1;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];
//    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, 75, 30)];
////    NSArray *imageArray = [_dataModel.sites objectAtIndex:section];
//    NSString *img = [NSString stringWithFormat:@"logo.%@.png",[_dataModel.sites objectAtIndex:section]];
//    myImageView.image = [UIImage imageNamed:img];
//    [myView addSubview:myImageView];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 30)];
    NSString *key = [_dataModel.sites objectAtIndex:section];
    NSString *name = [NSString stringWithFormat:@"-->>来自:%@",[_dataModel.sitetotitle objectForKey:key]];
    nameLabel.text = name;
    nameLabel.textColor = [UIColor redColor
                           ];
    [myView addSubview:nameLabel];
    
    NSArray *array = [_dataModel.submovies objectForKey:key];
    self.gao = array.count / 4.0 * 35.0 + 80;
    if (array.count == 0) {
        return nil;
    }
    
    return myView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSString *key = [_dataModel.sites objectAtIndex:section];
    NSArray *array = [_dataModel.submovies objectForKey:key];
    self.gao = array.count / 4.0 * 35.0 + 35;
    if (array.count == 0) {
        return 0.0;
    }
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_dataModel.type isEqualToString:@"teleplay"]) {
        
        static NSString *cellIdentifier = @"teleplayCell";
        TeleplayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[TeleplayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    
        NSString *key = [_dataModel.sites objectAtIndex:indexPath.section];
        cell.movieArray = [_dataModel.submovies objectForKey:key];
        cell.heider = _gao;
        cell.Delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataModel = _dataModel;
        return cell;

    }

    static NSString *cellIdentifier = @"classifyCell";
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[MovieTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSString *key = [_dataModel.sites objectAtIndex:indexPath.row];
    NSArray *array = [_dataModel.submovies objectForKey:key];
    cell.videoArray = array;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.Delegate = self;
    cell.dataModel = _dataModel;

    return cell;
}

//让UITableView的section header view不悬停的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_dataModel.type isEqualToString:@"teleplay"]) {
       NSString *key = [_dataModel.sites objectAtIndex:indexPath.row];
       NSArray *array = [_dataModel.submovies objectForKey:key];
       self.gao = array.count / 4.0 * 35.0 + 35;
       return _gao;
    }
    NSString *key = [_dataModel.sites objectAtIndex:indexPath.row];
    NSArray *array = [_dataModel.submovies objectForKey:key];
    self.gao = array.count * 35;
    return _gao;
}

-(void)getMessage:(NSString *)url :(NSString *)title
{
    MoviePlayerViewController *moviePlayeVC = [[MoviePlayerViewController alloc] init];
    moviePlayeVC.url = url;
    moviePlayeVC.titleName = title;
    [self presentViewController:moviePlayeVC animated:YES completion:^{
    
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
